/////////////////////////////////////////////////////////////////////
//
//
/////////////////////////////////////////////////////////////////////

#pragma once

#include <string>
#include <vector>

#define MAX_SQL_LENGTH			512

class ST_DataBase;

using namespace std;

class ST_Sql
{
public:
	ST_Sql(ST_DataBase*	pDataBase);
	virtual ~ST_Sql();

public:
	//
	bool BuildSQL(const char* szSQL,...);
	//
	const char* GetSQL() const;

public:
	virtual int FetchResult(int argc, char **argv, char **azColName);

protected:
	ST_DataBase*	m_pDataBase;

	char			m_szSQL[MAX_SQL_LENGTH + 1];
};

class ST_Sql_CheckAccount : public ST_Sql
{
public:
	struct ST_RowStruct
	{
		int		m_iID;
		short	m_iPrivilege;

		ST_RowStruct()
		{
			m_iID = 0;
			m_iPrivilege = -1;
		}
	};

public:
	ST_Sql_CheckAccount(ST_DataBase*	pDataBase);
	virtual ~ST_Sql_CheckAccount();

public:
	//
	bool	GetAccountInfo(const char* pszAccount,const char* pszPassword);
	//
	const ST_RowStruct&	GetRowStruct() const;

public:
	virtual int FetchResult(int iResultCount, char **azColVal, char **azColName);

private:
	bool			m_bValid;
	ST_RowStruct	m_RowInfo;
};

class ST_Sql_ChangePwd : public ST_Sql
{
public:
	ST_Sql_ChangePwd(ST_DataBase*	pDataBase);
	virtual ~ST_Sql_ChangePwd();

public:
	//
	bool	ChangePwd(int iID,const char* pszNewPwd);
};


class ST_Sql_AddNewGood : public ST_Sql
{
public:
	ST_Sql_AddNewGood(ST_DataBase*	pDataBase);
	virtual ~ST_Sql_AddNewGood();

public:
	//
	bool	AddNewGood(const char* pszGoodName,const char* pszProduct,const char* pszUnit,float fPrice);
};

class ST_Sql_DelGood : public ST_Sql
{
public:
	ST_Sql_DelGood(ST_DataBase*	pDataBase);
	virtual ~ST_Sql_DelGood();

public:
	//
	bool	DelGood(const char* pszGoodName);
};

class ST_Sql_LoadGoodList : public ST_Sql
{
public:
	struct ST_RowStruct
	{
		int		m_iGID;
		string	m_sGoodName;
		string	m_sProduct;
		string	m_sGoodUnit;
		float	m_fPrice;

		ST_RowStruct()
		{
			m_iGID = 0;
			m_fPrice = 0.0f;
		}
	};

public:
	ST_Sql_LoadGoodList(ST_DataBase*	pDataBase);
	virtual ~ST_Sql_LoadGoodList();

public:
	//
	bool	LoadGoodList();
	//
	bool	FilterGoodByName(const char* pszKey);
	//
	bool	LoadGoodByName(const char* pszName);
	//
	bool	LoadGoodByID(int iGID);
	//
	const vector<ST_RowStruct>& GetGoodLst() const;

public:
	virtual int FetchResult(int iResultCount, char **azColVal, char **azColName);

private:
	vector<ST_RowStruct>	m_RowVec;
};

class ST_Sql_AddPurchaseInfo : public ST_Sql
{
public:
	ST_Sql_AddPurchaseInfo(ST_DataBase*	pDataBase);
	virtual ~ST_Sql_AddPurchaseInfo();

public:
	//
	bool	AddPurchaseInfo(int iGoodID,float fNum,float fUnitPrice,float fTotalPrice,const char* pszBuyer,const char* pszSeller);
};

class ST_Sql_LoadPurchaseList : public ST_Sql
{
public:
	struct ST_RowStruct
	{
		int		m_iPID;
		int		m_iGID;
		float	m_fNum;
		float	m_fUnitPrice;
		float	m_fTotalPrice;
		string	m_sBuyer;
		string	m_sSeller;
		string	m_sTime;
		ST_RowStruct()
		{
			m_iPID = 0;
			m_iGID = 0;
			m_fNum = 0.0f;
			m_fUnitPrice = 0.0f;
			m_fTotalPrice = 0.0f;
		}
	};

public:
	ST_Sql_LoadPurchaseList(ST_DataBase*	pDataBase);
	virtual ~ST_Sql_LoadPurchaseList();

public:
	//
	bool	LoadPurchaseList();
	//
	const vector<ST_RowStruct>& GetPurchaseLst() const;

public:
	virtual int FetchResult(int iResultCount, char **azColVal, char **azColName);

private:
	bool					m_bValid;
	vector<ST_RowStruct>	m_RowVec;
};
